<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CommentService
 *
 * @author Administrator
 */
class CommentService {

    function create() {
        return D('comment')->create();
    }

    function insert($comment) {
        return D('comment')->add($comment);
    }

    function update($comment) {
        return D('comment')->save($comment);
    }

    function updateBySql($sql) {
        return D('comment')->execute($sql);
    }

    function delete($id) {
        $where['id'] = array('eq', $id);

        return D('comment')->where($where)->delete();
    }

    function listCommentByAppId($id, $limit, $field = 'comment.*, u.nick_name, u.avatar', $order = 'datetime desc') {
        $where['comment.app_id'] = array('eq', $id);
        return D('comment')->join('user u on comment.user_id = u.id')->field($field)->where($where)->limit($limit)->order($order)->select();
    }

    function findCommentByUIdAId($uId, $aId) {
        $where['app_id'] = array('eq', $aId);
        $where['user_id'] = array('eq', $uId);

        return D('comment')->where($where)->find();
    }

    function countCommentByAId($aId) {
        $where['app_id'] = array('eq', $aId);

        return D('comment')->where($where)->count();
    }

    function listCommentByAId($aId, $field, $limit, $order = 'datetime desc') {
        $where['app_id'] = array('eq', $aId);

        return D('comment')->join('user u on comment.user_id = u.id')->field($field)->where($where)->limit($limit)->order($order)->select();
    }

    function countCommentByUId($uId) {
        $where['user_id'] = array('eq', $uId);

        return D('comment')->where($where)->count();
    }

    function listCommentByUId($uId, $field, $limit, $order = 'datetime desc') {
        $where['user_id'] = array('eq', $uId);

        return D('comment')->join('app a on comment.app_id = a.id')->field($field)->where($where)->limit($limit)->order($order)->select();
    }

    function findCommentById($id) {
        $where['id'] = array('eq', $id);

        return D('comment')->where($where)->find();
    }

    function findAppCommentById($id, $field='a.name, comment.*') {
        $where['comment.id'] = array('eq', $id);

        return D('comment')->join('app a on a.id = comment.app_id')->field($field)->where($where)->find();
    }

}

?>
